﻿using AMEE.BeinControl;
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Input;

namespace BeinDemo1.ViewModel
{
    /// <summary>
    /// 
    /// </summary>
    class StepperVM : ViewModelBase<Stepper>
    {
        #region Fields

        RelayCommand _runCommand;
        RelayCommand _shutDownCommand;
        /// <summary>indicates, if RunCommand is being executed.</summary>
        bool _isRunning;

        #endregion


        #region Properties

        /// <summary>
        /// Gets the run command.
        /// </summary>
        public ICommand RunCommand {
            get {
                if (_runCommand == null) {
                    _runCommand = new RelayCommand(param => runExecute(param), pred => runCanExecute());
                }
                return _runCommand;
            }
        }

        /// <summary>
        /// Gets the shut down command.
        /// </summary>
        public ICommand ShutDownCommand {
            get {
                if (_shutDownCommand == null) {
                    _shutDownCommand = new RelayCommand(param => shutDownExecute(), pred => shutDownCanExecute());
                }
                return _shutDownCommand;
            }
        }

        #endregion


        #region Constructor

        /// <summary>
        /// Initializes a new instance of the <see cref="StepperVM"/> class.
        /// </summary>
        /// <param name="model">The model.</param>
        /// <exception cref="System.ArgumentNullException"></exception>
        public StepperVM(Stepper model)
            : base(model) {
                if (null == base.Model)
                    throw new ArgumentNullException();
        }

        #endregion


        #region Helper / Private

        /// <summary>
        /// Indicates, if RunCommand can be executed.
        /// </summary>
        /// <returns></returns>
        bool runCanExecute() {
            return _isRunning;
        }

        /// <summary>
        /// Executes the RunCommand (asynchronously).
        /// </summary>
        /// <param name="param">String "One": ein Schrittzyklus. String "Multiple": 5 Zyklen</param>
        void runExecute(object param) {
            string para = param as String;
            if (para == null) {
                Debug.WriteLine("StepperVM.runExecute - parameter ist kein String", "Warning");
                return;
            }
            int count = 0;
            switch (para) {
                case "One":
                    count = 1;
                    break;
                case "Multiple":
                    count = 5;
                    break;
                default:
                    break;
            }
            var task = new Task(i => performSteps(i), count);
            task.ContinueWith(tsk => _isRunning = false);
            task.Start();
        }

        /// <summary>
        /// Call of  PerformStepCyclus() <c>count</c> times.
        /// </summary>
        /// <param name="count">an Integer</param>
        private void performSteps(object count) {
            if (count is int) {
                int cnt = (int)count;
                this._isRunning = true;
                for (int i = 0; i < cnt; i++) {
                    this.Model.PerformStepCyclus();
                }
            }
        }
        

        bool shutDownCanExecute() {
            return true;
        }

        void shutDownExecute() {
            System.Windows.MessageBox.Show("ShutDown Befehl erhalten.");
        }


        #endregion

    }
}
